package com.navinfo.opentsp.platform.computing.serializer;

import com.alibaba.fastjson.JSON;
import com.google.common.base.Charsets;
import com.navinfo.opentsp.platform.computing.dto.*;
import com.navinfo.opentsp.platform.computing.util.DateUtils;
import com.navinfo.opentsp.platform.location.protocol.common.LCPredictiveMaintenanceD00B;
import org.apache.flume.Context;
import org.apache.flume.Event;
import org.apache.flume.serialization.EventSerializer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.OutputStream;

/**
* predictiveMaintenance信息
* HDFS_Sink的predictiveMaintenance序列化器，拼接成字段间通过\001分割的字符串
*/
public class HDFSPredictiveMaintenanceD00BSerializer implements EventSerializer {
    private static final Logger logger = LoggerFactory.getLogger(HDFSPredictiveMaintenanceD00BSerializer.class);

    private final String APPEND_NEWLINE = "appendNewline";
    private final boolean appendNewline;
    private final String splitStr = "\001";
    private final OutputStream out;

    private HDFSPredictiveMaintenanceD00BSerializer(OutputStream out, Context ctx) {
        this.appendNewline = ctx.getBoolean(APPEND_NEWLINE, true);
        this.out = out;
    }

    @Override
    public boolean supportsReopen() {
        return false;
    }
    @Override
    public void afterCreate() {}
    @Override
    public void afterReopen() {}
    @Override
    public void beforeClose() {}
    @Override
    public void flush() throws IOException {
        this.out.flush();
    }
    @Override
    public void write(Event event) throws IOException {
        try {
            //拼写落盘到HDFS的字符串。字段间分隔符是\001，null通过\N表示
            LCPredictiveMaintenanceD00B.PredictiveMaintenanceD00B predictiveMaintenance = LCPredictiveMaintenanceD00B.PredictiveMaintenanceD00B.parseFrom(event.getBody());
            StringBuilder predictiveMaintenanceStr = new StringBuilder(5000);
                    predictiveMaintenanceStr.append(predictiveMaintenance.getLatitude()).append(splitStr);
                    predictiveMaintenanceStr.append(predictiveMaintenance.getLongitude()).append(splitStr);
                    predictiveMaintenanceStr.append(predictiveMaintenance.getOriginalLat()).append(splitStr);
                    predictiveMaintenanceStr.append(predictiveMaintenance.getOriginalLng()).append(splitStr);
                    predictiveMaintenanceStr.append(predictiveMaintenance.getHeight()).append(splitStr);
                    predictiveMaintenanceStr.append(predictiveMaintenance.getDirection()).append(splitStr);
                    predictiveMaintenanceStr.append(predictiveMaintenance.getGpsDate()).append(splitStr);
                    predictiveMaintenanceStr.append(predictiveMaintenance.getVersion()).append(splitStr);
                    predictiveMaintenanceStr.append(JSON.toJSON(MaintenanceEvent1D00BDto.parseString(predictiveMaintenance.getMaintenanceEvent1()))).append(splitStr);
                    predictiveMaintenanceStr.append(JSON.toJSON(MaintenanceEvent2D00BDto.parseString(predictiveMaintenance.getMaintenanceEvent2()))).append(splitStr);
                    predictiveMaintenanceStr.append(JSON.toJSON(MaintenanceEvent3D00BDto.parseString(predictiveMaintenance.getMaintenanceEvent3()))).append(splitStr);
                    predictiveMaintenanceStr.append(JSON.toJSON(MaintenanceEvent4D00BDto.parseString(predictiveMaintenance.getMaintenanceEvent4()))).append(splitStr);
                    predictiveMaintenanceStr.append(JSON.toJSON(MaintenanceEvent5D00BDto.parseString(predictiveMaintenance.getMaintenanceEvent5()))).append(splitStr);
                    predictiveMaintenanceStr.append(JSON.toJSON(MaintenanceEvent6D00BDto.parseString(predictiveMaintenance.getMaintenanceEvent6()))).append(splitStr);
                    predictiveMaintenanceStr.append(predictiveMaintenance.getTerminalId()).append(splitStr);
                    predictiveMaintenanceStr.append(predictiveMaintenance.getSerialNumber()).append(splitStr);

//            predictiveMaintenanceStr.append(predictiveMaintenance.getTerminalId()).append(splitStr);

            //添加字段只能在分割线之上添加,注意时间分区这块，终端传的时候是秒还是毫秒
            //------------------------------------------分割线----------------------------------------
            predictiveMaintenanceStr.append(DateUtils.format(predictiveMaintenance.getGpsDate() * 1000, "yyyyMMdd")).append(splitStr);

            predictiveMaintenanceStr.append(predictiveMaintenance.getTerminalId()%32);


            //将数据拼接后写入hdfs
            this.out.write(predictiveMaintenanceStr.toString().getBytes(Charsets.UTF_8));
            if (this.appendNewline) {
                this.out.write(10);
            }
        }catch (Exception e){
            logger.error("执行HDFS的HDFSPredictiveMaintenanceSerializer拦截器失败：" + e.getMessage(), e);
        }
    }

//    public static List<String> getMaintenanceEvents(List<LCLocationData.StaytimeParkingAddition> staytimeParkings){
//        List<String> staytimeParkingDtos = Lists.newArrayList();
//        if(staytimeParkings == null){
//            return staytimeParkingDtos;
//        }
//        for(LCLocationData.StaytimeParkingAddition staytimeParking : staytimeParkings){
//            staytimeParkingDtos.add(StaytimeParkingAdditionDto.parseString(staytimeParking));
//        }
//        return staytimeParkingDtos;
//    }


    public static class Builder implements EventSerializer.Builder {
        public Builder() { }
        @Override
        public EventSerializer build(Context context, OutputStream out) {
            return  new HDFSPredictiveMaintenanceD00BSerializer(out, context);
        }
    }
}
